package com.example.pet.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.pet.config.Encryption;
import com.example.pet.entity.Photo;
import com.example.pet.entity.User;
import com.example.pet.entity.UserOne;
import com.example.pet.service.PhotoService;
import com.example.pet.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "用户(登录/注册)")
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;
    @Resource
    private PhotoService photoService;
    @ApiOperation("注册")
   @PostMapping("/enroll")
    public String enroll(User user){
        User user1 = userService.selectOneByNameAndPassword(user);
        boolean add = false;
        if(user1==null){
            String MD = Encryption.hashMD5(user.getPassword());
            user.setPassword(MD);
            add = userService.add(user);
            User login = userService.login(user);
            if(login!=null){
                Photo photo = new Photo(login.getUserId(),"http://43.136.137.117:5000/toxiang.jpg","李四");
                photoService.add(photo);
            }
        }
       return add?"注册成功":"注册失败,手机号已存在";
   }
    @ApiOperation("登录")
    @PostMapping("/login")
    public UserOne login(User user){
        UserOne userOne = new UserOne();
        String MD = Encryption.hashMD5(user.getPassword());
        user.setPassword(MD);
        User user1 = userService.login(user);
        if(user1!=null){
            User user2 = userService.selectOneByNameAndPassword(user);
            userOne.setSc("登录成功");
            userOne.setUserId(user2.getUserId());
            return userOne;
        }
        userOne.setSc("登录失败，账号或密码有误");
        return userOne;
    }
}

